package com.ohuang.wallpapermanager.compose

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.net.Uri
import android.os.SystemClock
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.View.OnClickListener
import android.view.View.OnTouchListener
import android.view.ViewGroup
import android.widget.Toast




fun toColorInt(num: Number): Int {
    return num.toInt()
}

fun Number.toColor(): Int {
    return toInt()
}

fun Context.toastShort(string: String) {
    Toast.makeText(this, string, Toast.LENGTH_SHORT).show()
}

fun Context.toastLong(string: String) {
    Toast.makeText(this, string, Toast.LENGTH_LONG).show()
}

fun  Context.startUrlForBrowser(url: String) {
    var intent = Intent(Intent.ACTION_VIEW)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.addCategory(Intent.CATEGORY_BROWSABLE)
    intent.data = Uri.parse(url)
    startActivity(intent)
}




fun View.setSafeClickListener(delay: Long = 1000, listener: View.OnClickListener) {
    setOnClickListener(object : OnClickListener {
        var lastClickTime = 0L
        override fun onClick(v: View?) {
            var uptimeMillis = SystemClock.uptimeMillis()
            if (uptimeMillis - lastClickTime > delay) {
                lastClickTime = uptimeMillis
                listener.onClick(v)
            }
        }

    })
}

/**
 * 若view已设置setonClickListener  onTouchReturn需要设置成false避免事件setonClickListener被拦截
 */
fun View.setTouchClickListener(onTouchReturn: Boolean = true, listener: OnClickListener) {

    setOnTouchListener(object : OnTouchListener {

        var canClick = true
        override fun onTouch(v: View?, event: MotionEvent): Boolean {
            val x = event.x
            val y = event.y
            var overstep = true

            when (event.action) {
                MotionEvent.ACTION_DOWN -> {

                }
                MotionEvent.ACTION_MOVE -> if (canClick) {
                    if (onTouchReturn) { //onTouchReturn为ture的时候才需要做边界判断
                        v?.let {
                            overstep =
                                (x >= -1) && (y >= -1) && (x <= width + 1) && (y <= height + 1)
                        }
                    }
                    canClick = overstep
                }
                MotionEvent.ACTION_UP -> {
                    if (canClick) {
                        canClick = overstep
                        if (canClick) {
                            listener.onClick(v)
                        }
                    }
                    canClick = true
                }
            }
            return onTouchReturn && canClick
        }
    })
}

fun View.setSize(w: Int, h: Int) {
    var lb = layoutParams
    if (lb == null) {
        lb = ViewGroup.MarginLayoutParams(w, h)
    }
    lb.width = w
    lb.height = h
    layoutParams = lb
}

fun View.setWidthSize(w: Int) {
    var lb = layoutParams
    if (lb == null) {
        lb = ViewGroup.MarginLayoutParams(w, -2)
    }
    lb.width = w
    layoutParams = lb
}

fun View.setHeightSize(h: Int) {
    var lb = layoutParams
    if (lb == null) {
        lb = ViewGroup.MarginLayoutParams(-2, h)
    }
    lb.height = h
    layoutParams = lb
}

fun View.setMarginTop(i: Int) {
    var lb = layoutParams
    if (lb is ViewGroup.MarginLayoutParams) {
        lb.topMargin = i
    }
}

fun View.setMarginBottom(i: Int) {
    var lb = layoutParams
    if (lb is ViewGroup.MarginLayoutParams) {
        lb.bottomMargin = i
    }
}

fun View.setMarginLeft(i: Int) {
    var lb = layoutParams
    if (lb is ViewGroup.MarginLayoutParams) {
        lb.leftMargin = i
    }
}

fun View.setMarginRight(i: Int) {
    var lb = layoutParams
    if (lb is ViewGroup.MarginLayoutParams) {
        lb.rightMargin = i
    }
}

fun View.setPaddingLeft(int: Int) {
    setPadding(int, paddingTop, paddingRight, paddingBottom)
}

fun View.setPaddingTop(int: Int) {
    setPadding(paddingLeft, int, paddingRight, paddingBottom)
}

fun View.setPaddingRight(int: Int) {
    setPadding(paddingLeft, paddingTop, int, paddingBottom)
}

fun View.setPaddingBottom(int: Int) {
    setPadding(paddingLeft, paddingTop, paddingRight, int)
}

fun Context.startActivity(activity: Class<out Activity>) {
    var intent = Intent(this, activity)
    if (this is Application) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }
    startActivity(intent)
}




fun Context.createView(layoutId: Int): View {
    return LayoutInflater.from(this).inflate(layoutId, null)
}

fun ViewGroup.createView(layoutId: Int): View {
    return LayoutInflater.from(this.context).inflate(layoutId, this, false)
}



fun Number.dp2px(): Int {
    val scale = Resources.getSystem().displayMetrics.density
    val toDouble = this.toDouble()
    return (toDouble * scale + 0.5f).toInt()
}

fun Number.dp2px(context: Context): Int {
    val scale = context.getResources().getDisplayMetrics().density
    val toDouble = this.toDouble()
    return (toDouble * scale + 0.5f).toInt()
}


fun Number.px2dp(): Int {
    val scale = Resources.getSystem().displayMetrics.density
    val toDouble = this.toDouble()
    return (toDouble / scale + 0.5f).toInt()
}

fun Number.px2dp(context: Context): Int {
    val scale = context.getResources().getDisplayMetrics().density
    val toDouble = this.toDouble()
    return (toDouble / scale + 0.5f).toInt()
}


fun Number.sp2px(): Int {
    val fontScale = Resources.getSystem().displayMetrics.scaledDensity
    val toDouble = this.toDouble()
    return (toDouble * fontScale + 0.5f).toInt()
}

fun Number.sp2px(context: Context): Int {
    val fontScale = context.getResources().getDisplayMetrics().density
    val toDouble = this.toDouble()
    return (toDouble * fontScale + 0.5f).toInt()
}


fun Number.px2sp(): Int {
    val fontScale = Resources.getSystem().displayMetrics.scaledDensity
    val toDouble = this.toDouble()
    return (toDouble / fontScale + 0.5f).toInt()
}

fun Number.px2sp(context: Context): Int {
    val fontScale = context.getResources().getDisplayMetrics().density
    val toDouble = this.toDouble()
    return (toDouble / fontScale + 0.5f).toInt()
}


fun Number.dp2sp(context: Context):Int{
    return dp2px(context).px2sp(context)
}


inline fun tryCatch(block: () -> Unit) {
    try {
        block()
    } catch (_: Throwable) {
    }
}

inline fun tryCatch(block: () -> Unit, error: (Throwable) -> Unit) {
    try {
        block()
    } catch (e: Throwable) {
        error(e)
    }
}

inline fun tryCatchPrint(block: () -> Unit) {
    try {
        block()
    } catch (e: Throwable) {
        e.printStackTrace()
    }
}

inline fun tryCatchPrint(block: () -> Unit, finally: () -> Unit) {
    try {
        block()
    } catch (e: Throwable) {
        e.printStackTrace()
    } finally {
        finally()
    }
}







